

clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user

set graphics on  
 
if "$user" == "C:\Users\jwohlfar"{
global path_data_processed "C:\Users\jwohlfar\AppData\Local\Dropbox\Daten\Dropbox\Sentiment analysis_household finance\Aggregate unemployment and expectations formation\replication files for RESTAT\data\processed"
global path_output "C:\Users\jwohlfar\AppData\Local\Dropbox\Daten\Dropbox\Sentiment analysis_household finance\Aggregate unemployment and expectations formation\replication files for RESTAT\output"
}
 
clear all
set more off

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
 

gen ind_cyclical=1-health_educ-ind_other
tab ind_cyclical, m
gen ind_noncyclical=1-ind_cyclical
 
foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1
global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index
	   
gen posterior_educ = posterior*atleast_bachelor
gen treatment_educ = treatment*atleast_bachelor

gen posterior_old = posterior*old
gen treatment_old = treatment*old

gen posterior_female = 	posterior*female
gen treatment_female = treatment*female

gen posterior_highearn = posterior*highearn
gen treatment_highearn = treatment*highearn

gen posterior_ever_unemp = posterior*ever_unemployed_imp
gen treatment_ever_unemp = treatment*ever_unemployed_imp
gen posterior_never_unemp = posterior*never_unemployed_imp
gen treatment_never_unemp = treatment*never_unemployed_imp

gen posterior_highcountyunemp = posterior*highunemp_rate3y
gen treatment_highcountyunemp = treatment*highunemp_rate3y
gen posterior_lowcountyunemp = posterior*(1-highunemp_rate3y)
gen treatment_lowcountyunemp = treatment*(1-highunemp_rate3y)

gen posterior_ind_cyclical = posterior*ind_cyclical
gen treatment_ind_cyclical = treatment*ind_cyclical
gen posterior_ind_noncyclical = posterior*(1-ind_cyclical)
gen treatment_ind_noncyclical = treatment*(1-ind_cyclical)

gen posterior_finlit_index = posterior*finlit_index
gen treatment_finlit_index = treatment*finlit_index


gen posterior_republican = posterior*republican
gen treatment_republican = treatment*republican
 
 

			   loc experiments "infl_sd infl_sd infl_sd infl_sd infl_sd infl_sd infl_sd infl_sd"

			   
			   
		  
preserve

clear all
eststo clear
estimates drop _all

set obs 10
qui gen x = 1
qui gen y = 1

loc columns = 0

foreach choice in `experiments' {

    loc ++columns
    qui eststo col`columns': ivreg2 x y

}

restore


/* Statistics */

loc colnum = 1
loc colnames ""



 
*** Column 1: Education

ivreg2 infl_sd (posterior posterior_educ = treatment treatment_educ) prior $controls, r

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_educ, prec(3)
estadd loc thisstat5 = "`r(bstar)'": col`colnum'
estadd loc thisstat6 = "`r(sestar)'": col`colnum'

test posterior + posterior_educ = 0
estadd loc thisstat29 = string(r(p), "%9.3f"): col`colnum'

qui sum infl_sd
estadd loc thisstat31 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 2: Earnings

ivreg2 infl_sd (posterior posterior_highearn = treatment treatment_highearn) prior $controls highearn, r

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_highearn, prec(3)
estadd loc thisstat8 = "`r(bstar)'": col`colnum'
estadd loc thisstat9 = "`r(sestar)'": col`colnum'

test posterior + posterior_highearn = 0
estadd loc thisstat29 = string(r(p), "%9.3f"): col`colnum'

qui sum infl_sd
estadd loc thisstat31 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 3: Age

ivreg2 infl_sd (posterior posterior_old = treatment treatment_old) prior $controls old, r

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_old, prec(3)
estadd loc thisstat11 = "`r(bstar)'": col`colnum'
estadd loc thisstat12 = "`r(sestar)'": col`colnum'

test posterior + posterior_old = 0
estadd loc thisstat29 = string(r(p), "%9.3f"): col`colnum'

qui sum infl_sd
estadd loc thisstat31 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 4: Gender

ivreg2 infl_sd (posterior posterior_female = treatment treatment_female) prior $controls, r

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_female, prec(3)
estadd loc thisstat14 = "`r(bstar)'": col`colnum'
estadd loc thisstat15 = "`r(sestar)'": col`colnum'

test posterior + posterior_female = 0
estadd loc thisstat29 = string(r(p), "%9.3f"): col`colnum'

qui sum infl_sd
estadd loc thisstat31 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 5: Industry

ivreg2 infl_sd (posterior posterior_ind_noncyclical = treatment treatment_ind_noncyclical) ind_cyclical prior $controls, r

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_ind_noncyclical, prec(3)
estadd loc thisstat17 = "`r(bstar)'": col`colnum'
estadd loc thisstat18 = "`r(sestar)'": col`colnum'

test posterior + posterior_ind_noncyclical = 0
estadd loc thisstat29 = string(r(p), "%9.3f"): col`colnum'

qui sum infl_sd if ind_cyclical!=.
estadd loc thisstat31 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 6: Personal unemployment history

ivreg2 infl_sd (posterior posterior_never_unemp = treatment treatment_never_unemp) never_unemployed_imp prior $controls, r

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_never_unemp, prec(3)
estadd loc thisstat20 = "`r(bstar)'": col`colnum'
estadd loc thisstat21 = "`r(sestar)'": col`colnum'
	
test 	posterior + posterior_never_unemp = 0
estadd loc thisstat29 = string(r(p), "%9.3f"): col`colnum'

qui sum infl_sd if never_unemployed_imp!=.
estadd loc thisstat31 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	

 loc ++colnum


*** Column 7: County-level unemployment

ivreg2 infl_sd (posterior posterior_lowcountyunemp = treatment treatment_lowcountyunemp) highunemp_rate3y prior $controls, r

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_lowcountyunemp, prec(3)
estadd loc thisstat23 = "`r(bstar)'": col`colnum'
estadd loc thisstat24 = "`r(sestar)'": col`colnum'

test 	posterior + posterior_lowcountyunemp = 0
estadd loc thisstat29 = string(r(p), "%9.3f"): col`colnum'

qui sum infl_sd if highunemp_rate3y!=.
estadd loc thisstat31 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	

 loc ++colnum


*** Column 8: financial literacy.

ivreg2 infl_sd (posterior posterior_finlit_index = treatment treatment_finlit_index) finlit_index prior $controls, r

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_finlit_index, prec(3)
estadd loc thisstat26 = "`r(bstar)'": col`colnum'
estadd loc thisstat27 = "`r(sestar)'": col`colnum'

test 	posterior + posterior_finlit_index = 0
estadd loc thisstat29 = string(r(p), "%9.3f"): col`colnum'

qui sum infl_sd if finlit_index!=.
estadd loc thisstat31 = r(N): col`colnum'

 
 


loc rowlabels " " " "Posterior (a)" " " " " "Posterior $\times$ (b) " "High education" " " "Posterior $\times$ (b) " "High earnings" " " "Posterior $\times$ (b) " "Age$>$44" " " "Posterior $\times$ (b) " "Female" " " "Posterior $\times$ (b) " "Non-cyclical industry" " " "Posterior $\times$ (b) " "Never unemployed"   " " "Posterior $\times$ (b) " "Low county unemployment" " " "Posterior $\times$ (b) " "High financial literacy" " " "Pr(a+b)=0" " " "Observations" " " "\hline" " "   "

loc rowstats ""
loc rowstats ""

forval i = 1/31 {
loc rowstats "`rowstats' thisstat`i'"
}

esttab * using "inflation_sd_hetero.tex", replace cells(none) booktabs nonotes nomtitles compress alignment(c) nogap noobs nobaselevels label stats(`rowstats', labels(`rowlabels')) ///
mgroups("\shortstack{Inflation:\\Uncertainty}"  , pattern(1 0 0 0 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

eststo clear	






